﻿using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System;
namespace Inovout.Business
{
    public class FunctionGroup : IFunction
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string hotKey;
        public string HotKey
        {
            get { return hotKey; }
            set { hotKey = value; }
        }
        private IList<IFunction> functions = new List<IFunction>();
        public IList<IFunction> Functions
        {
            get { return functions; }
            private set { functions = value; ; }
        }

        private FunctionGroup parent;
        public FunctionGroup Parent
        {
            get { return parent; }
            private set { parent = value; }
        }

        private FunctionGroup(string name, FunctionGroup parent)
        {
            this.name = name;
            if (parent != null)
            {
                parent.AddFunction(this);
            }
        }

        public FunctionGroup(FunctionGroup parent)
        {
            parent.AddFunction(this);
        }
       
        protected Function NewFunction(FunctionGroup parentFunctionGroup, Type workItemType, Function dependentFunction=null)
        {
            Function function = new Function(this, workItemType, dependentFunction);
            parentFunctionGroup.AddFunction(function);
            return function;
        }
        protected Function NewFunction(Type workItemType, Function dependentFunction)
        {
            return NewFunction(this, workItemType, dependentFunction);
        }
        public void AddFunction(IFunction function)
        {
            FunctionGroup functionGroup = function as FunctionGroup;
            if (functionGroup != null)
            {
                functionGroup.Parent = this;
            }
            else
            {
                Function fun = function as Function;
                fun.Parent = this;
            }
            functions.Add(function);
        }

        //private static IEnumerable<FunctionGroup> validFunctionGroups;
        //public static IEnumerable<FunctionGroup> ValidFunctionGroups
        //{
        //    get
        //    {

        //        if (validFunctionGroups == null)
        //        {
        //            FunctionGroup clonedRoot = new FunctionGroup();
        //            clonedRoot.Functions = new List<IFunction>(Root.Functions as IList<IFunction>);
        //            RemoveEmptyFunctionGroup(clonedRoot);
        //            validFunctionGroups = clonedRoot.Functions.Cast<FunctionGroup>();
        //        }
        //        return validFunctionGroups;
        //    }
        //}

        private static void RemoveEmptyFunctionGroup(FunctionGroup functionGroup)
        {
            int functionGroupCount = functionGroup.Functions.Count;
            for (int i = 0; i < functionGroupCount; i++)
            {
                if (functionGroup.Functions[0] is FunctionGroup)
                {
                    RemoveEmptyFunctionGroup(functionGroup.Functions[0] as FunctionGroup);
                    if (((FunctionGroup)functionGroup.Functions[0]).Functions.Count == 0)
                    {
                        functionGroup.Functions.RemoveAt(0);
                    }
                }
            }
        }

        [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        public static readonly FunctionGroup Application = new FunctionGroup("Application", null);
        [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        [Alias("文件")]
        public static readonly FunctionGroup File = new FunctionGroup("File", Application) { hotKey = "F" };
        [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        [Alias("视图")]
        public static readonly FunctionGroup View = new FunctionGroup("View", Application) { hotKey = "V" };
        [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        [Alias("设置")]
        public static readonly FunctionGroup Settings = new FunctionGroup("Settings", Application) { HotKey = "S" };
    }
}
